package com.snow.auth.controller;

import com.snow.auth.entity.SystemPostEntity;
import com.snow.auth.service.SystemPostService;
import com.snow.conditions.*;
import com.snow.jpa.model.BasePageParams;
import com.snow.jpa.model.IBaseSearchParams;
import com.snow.model.IdParams;
import com.snow.view.PageData;
import com.snow.view.WebResponse;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/system-post")
public class SystemPostController {

    private final SystemPostService systempostService;

    public SystemPostController(SystemPostService systempostService) {
        this.systempostService = systempostService;
    }

    @Getter
    @Setter
    @QueryDescription(sort = @Order(value = "createTime", type = QueryOrderType.desc))
    public static class PageParams extends BasePageParams {
        @QueryConditionField(type = ConditionType.like)
        private String name;
    }

    @PostMapping("/page")
    public WebResponse<PageData<SystemPostEntity>> page(@RequestBody PageParams params) {
        return WebResponse.success(systempostService.page(params));
    }

    @PostMapping("/create")
    public WebResponse<SystemPostEntity> create(@RequestBody SystemPostEntity entity) {
        return WebResponse.success(systempostService.save(entity));
    }

    @PostMapping("/update")
    public WebResponse<SystemPostEntity> update(@RequestBody SystemPostEntity entity) {
        return WebResponse.success(systempostService.update(entity));
    }

    @PostMapping("/delete")
    public WebResponse<SystemPostEntity> delete(@RequestBody IdParams params) {
        return WebResponse.success(systempostService.delete(params.getId()));
    }

    @Getter
    @Setter
    public static class SearchParams implements IBaseSearchParams {
        @QueryConditionField(type = ConditionType.like)
        private String name;
    }

    @PostMapping("/search")
    public WebResponse<List<SystemPostEntity>> search(@RequestBody SearchParams params) {
        return WebResponse.success(systempostService.search(params));
    }
}